<?php
namespace app\erp_meixingtiyu\model;

use think\Model;
use think\model\concern\SoftDelete;

use app\common\enum\order\PayStatus as OrderPayStatusEnum;
use \DateTime;

// 预订表和场次表的中间表，记录一个预订订单中所有预订的场次
class BookingFragment extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $table = 'athlon_arena_booking_fragment';

    public function booking() 
    {
        return $this->belongsTo(Booking::class);
    }

    public function getPrice($date, $part_time_fragment_id)
    {
        return self::where([
            'part_time_fragment_id' => $part_time_fragment_id
        ])
            ->whereTime('start_date', '<=', $date)
            ->whereTime('end_date', '>=', $date)
            ->order('update_time desc')
            ->find();
    }

    // 判断指定场地当前及未来是否有预订的场次
    public function haveBooking($arenaId)
    {
        $now = (new DateTime)->format('Y-m-d');
        return $this->where('arena_id', $arenaId)->where('book', 1)->where('pay_status', OrderPayStatusEnum::SUCCESS)->where('start_date', '>=', $now)->select();
    }
}
